package com.demo.cn.greendaodemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.demo.cn.greendaodemo.entity.User;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @butterknife.InjectView(R.id.btn1)
    TextView btn1;
    @butterknife.InjectView(R.id.btn2)
    TextView btn2;
    @butterknife.InjectView(R.id.btn3)
    TextView btn3;
    @butterknife.InjectView(R.id.btn4)
    TextView btn4;
    @butterknife.InjectView(R.id.result)
    TextView reult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        butterknife.ButterKnife.inject(this);
        initView();
        initDataBase();
    }

    private void initView() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }

    /**
     * 初始化数据库
     */
    private void initDataBase() {


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                User user = new User(1l,"耿清","hahahahaha");
                User user1 = new User(2l,"刘德华","天王");
                SQLiteUtils.getInstance().addContacts(user);
                SQLiteUtils.getInstance().addContacts(user1);
                break;
            case R.id.btn2:
                User user2 = new User(1l,"耿清","hahahahaha");
                String sqlDelete = "DELETE from USER WHERE USER.USERNAME=\"刘德华\"";

                SQLiteUtils.getInstance().getDaoSession().getDatabase().execSQL(sqlDelete);
                break;
            case R.id.btn3:
                String s = "update USER set NICENAME=\"001\" where USER.USERNAME=\"耿清\"";
                SQLiteUtils.getInstance().getDaoSession().getDatabase().execSQL(s);
                break;
            case R.id.btn4:
                List<User> list = SQLiteUtils.getInstance().selectAllContacts();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < list.size(); i++) {
                    sb.append(list.get(i).getUsername() +"--"+ list.get(i).getNicename()+"\n");
                }
                reult.setText(sb.toString());
                break;

        }

    }
}
